package com.fare.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringAndDateAndFileUtil {
	
	/**
	 * 格式化日期
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		if(null == date) return "";
		if(null == format || "".equals(format.trim()))
			format = "yyyy-MM-dd HH:mm:ss";
		return new SimpleDateFormat(format).format(date);
	}
	
	/**
	 * 格式化日期
	 * @param dateString
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date formatDateString(String dateString, String format) throws ParseException {
		if(checkString(dateString)){
			if(!checkString(format)){
				format = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(dateString);
		}
		return null;
	}
	
	 /**
     * 验证字符串不为空。
     * 
     * @param str
     * @return
     */
    public static boolean checkString(String str) {
        if (str != null && !"".equals(str.trim())) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * delete the file by its full path and name
     * @param fileFullName
     */
    public static void deleteFile (String fileFullName) {
        File file = new File(fileFullName);

        if (file.isFile()) {
            deleteFileOrDir(file);
        }

    }

    /**
     * Method description
     *
     *
     * @param file
     */
    public static void deleteFileOrDir (File file) {
        if (file.isDirectory()) {
            File[] subFiles = file.listFiles();

            if (subFiles.length > 0) {
                for (int i = 0; i < subFiles.length; i++) {
                    deleteFileOrDir(subFiles[i]);
                }
            }

            file.delete();
        }
        else {
            file.delete();
        }
    }
    
}
